package com.mcptt.config.body;

import android.util.Xml;

import com.cloudptt.api.product.log.Log;
import com.mcptt.config.model.ConferenceInfo;
import com.mcptt.db.model.AddrInfo;

import org.xmlpull.v1.XmlPullParser;

import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

public class ConferenceInfoBody {

	private static final String End_Tag = "</conference-info>";
	private static final String Name_Root = "conference-info";
	private static final String Name_Connect = "connected";
	private static final String Name_Subject = "subject";
	private static final String Name_Users = "users";
	private static final String Name_User = "user";
	private static final String Name_Endpoint  = "endpoint";
	private static final String Name_Display  = "display-text";
	private static final String Name_Status = "status";
	private static final String Name_State = "state";
	private static final String Name_ControllerPsi = "mcdata-controller-psi";



	private boolean isFull = false;
	private String  mcType = "mcptt";
	private Map<String,Boolean> conferenceInfoMap = new HashMap();

	public ConferenceInfoBody()
	{
	}




	public static ConferenceInfoBody parseXmlStr(String xmlStr)
	{
		ConferenceInfoBody body = new ConferenceInfoBody();
		int valType = 0;
//		String key = End_Mcptt_Tag;

		int index = xmlStr.indexOf(End_Tag);
		Log.debug(ConferenceInfoBody.class, "parseXmlStr index = " + index);

//	    if(index <= 0)
//	    {
//	    	key = End_Mcvideo_Tag;
//	    	index = xmlStr.indexOf(key);
//	    }
		xmlStr= xmlStr.substring(0, index + End_Tag.length());
		xmlStr = xmlStr.trim();
		String name = "";
		String status = "";
		try {
			ByteArrayInputStream strIs = new ByteArrayInputStream(xmlStr.getBytes());
			XmlPullParser pullParser = Xml.newPullParser();
			pullParser.setInput(strIs, "UTF-8"); //为Pull解释器设置要解析的XML数据
			int event = pullParser.getEventType();

			while (event != XmlPullParser.END_DOCUMENT) {

				switch (event) {

					case XmlPullParser.START_DOCUMENT:
						break;
					case XmlPullParser.START_TAG:
						Log.debug(ConferenceInfoBody.class,"pullParser.getName() = " + pullParser.getName());
						if (Name_Root.equals(pullParser.getName())) {
							String temp = pullParser.getAttributeValue("",Name_State);
							Log.debug(ConferenceInfoBody.class,"temp = " + temp);
							if(temp != null) {
								if (temp.toLowerCase().equals("full")) {
									body.isFull = true;
								} else {
									body.isFull = false;
								}
							}
						}
						else if (Name_Subject.equals(pullParser.getName())) {
							body.mcType = pullParser.nextText();
							Log.debug(ConferenceInfoBody.class,"mcType = " + body.mcType);
						} else if (Name_Users.equals(pullParser.getName())) {

						} else if (Name_Display.equals(pullParser.getName())) {
							String url = pullParser.nextText();
							name = AddrInfo.toNumFromSipUrl(url);
							Log.debug(ConferenceInfoBody.class,"name = " + name);
						} else if (Name_Status.equals(pullParser.getName())) {
							status= pullParser.nextText();
							Log.debug(ConferenceInfoBody.class,"status = " + status);
						}
						break;

					case XmlPullParser.END_TAG:
						if (Name_Endpoint.equals(pullParser.getName())) {
							boolean isConnect = isConnect(status);
							body.conferenceInfoMap.put(name,isConnect);
						}
						break;

				}

				event = pullParser.next();
			}
		}catch (Exception e)
		{
			e.printStackTrace();
		}

		return body;
	}

	private static boolean isConnect(String connectStr)
	{
		if(connectStr != null
				&& Name_Connect.equals(connectStr))
		{
			return true;
		}
		return false;
	}

	public Map<String, Boolean> getConferenceInfoMap() {
		return conferenceInfoMap;
	}
	public boolean isFull() {
		return isFull;
	}
	public String toString()
	{
		StringBuffer strBuf = new StringBuffer("McdataInfoBody ");
		strBuf.append("mcType = ").append(mcType).append(",");
		strBuf.append("isFull = ").append(isFull).append(",");
		strBuf.append("conferenceInfoMap = ").append(conferenceInfoMap).append(".");

		return strBuf.toString();
	}

}
